package com.zhiyume.service;

import com.zhiyume.dao.api.TAdminSelfMapper;
import com.zhiyume.dao.api.TMenuMapper;
import com.zhiyume.dao.entity.TAdminSys;
import com.zhiyume.dao.entity.TMenu;
import com.zhiyume.dao.entity.TRole;
import com.zhiyume.service.util.StringUtil;
import com.zhiyume.shiro.AdminSecurityManager;
import com.zhiyume.shiro.PermissionProcessor;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class AdminService {
    @Autowired
    private TAdminSelfMapper tAdminSelfMapper;
    @Autowired
    private TMenuMapper tMenuMapper;
    public TAdminSys selectAdminByUserName(String userName){
        return tAdminSelfMapper.selectAdminByUserName(userName);
    }

    public SimpleAuthorizationInfo simpleAuthorizationInfo(TAdminSys tAdmin) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        PermissionProcessor permissionProcessor = new PermissionProcessor();
        List<TRole> roleList = tAdminSelfMapper.selectUserRoleByUId(tAdmin.getId());
        List<String> roleCodeList = new ArrayList<>();
        List<TMenu> menuList = new ArrayList<TMenu>();
        if(null != roleList && roleList.size() > 0) {
            for(TRole tRole : roleList) {
                roleCodeList.add(tRole.getRoleCode());
                /*
                    系统管理员 赋予所有的权限
                 */
                if(AdminSecurityManager.ADMIN_ROLE_CODE.equals(tRole.getRoleCode())){
                    menuList = tMenuMapper.selectByExample(null);
                    permissionProcessor.addPermission("*");
                    break;
                }else {
                   List<TMenu> list = tAdminSelfMapper.selectMenuByRoId(tRole.getId());
                    if(null != list && list.size() >0) {
                        for(TMenu tMenu : list) {
                            if(!menuList.contains(tMenu)){
                                menuList.add(tMenu);
                            }
                            permissionProcessor.addPermission(tMenu.getMenuCode());
                        }
                    }

                }
            }
        }
        if(menuList.size()>0){
            List<TMenu> menus = new ArrayList<>();
            for(TMenu tMenu : menuList) {
                //添加所有的一级菜单
                if( null == tMenu.getMenuPid()){
                    menus.add(tMenu);
                }
            }
            // 为一级菜单设置子菜单，getChild是递归调用的
            for (TMenu menu : menuList) {
                menu.setChildren(getChild(menu.getId(), menuList));
            }
            tAdmin.setMenuList(menus);
        }
        authorizationInfo.addObjectPermissions(((Collection)permissionProcessor.getPermissions()));
        /**添加角色*/
        authorizationInfo.addRoles(roleCodeList);
        return authorizationInfo;
    }
    /**
     * 递归查找子菜单
     *
     * @param id
     *            当前菜单id
     * @param rootMenu
     *            要查找的列表
     * @return
     */
    private List<TMenu> getChild(Integer id, List<TMenu> rootMenu) {
        // 子菜单
        List<TMenu> childList = new ArrayList<>();
        for (TMenu menu : rootMenu) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            if (null != menu.getMenuPid()) {
                if (menu.getMenuPid().equals(id)) {
                    childList.add(menu);
                }
            }
        }
        // 把子菜单的子菜单再循环一遍
        for (TMenu menu : childList) {// 没有url子菜单还有子菜单
            if (StringUtil.isBlank(menu.getHref())) {
                // 递归
                menu.setChildren(getChild(menu.getId(), rootMenu));
            }
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }
}
